{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [hello paddle: 从普通程序走向机器学习程序](https://www.paddlepaddle.org.cn/documentation/docs/zh/practices/quick_start/hello_paddle.html)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12.0\n",
      "16.0\n",
      "20.0\n",
      "28.0\n",
      "30.0\n",
      "50.0\n"
     ]
    }
   ],
   "source": [
    "def calculate_fee(distance_travelled):\n",
    "    return 10 + 2 * distance_travelled\n",
    "\n",
    "\n",
    "for x in [1.0, 3.0, 5.0, 9.0, 10.0, 20.0]:\n",
    "    print(calculate_fee(x))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python3/dist-packages/urllib3/util/selectors.py:14: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working\n",
      "  from collections import namedtuple, Mapping\n",
      "/usr/lib/python3/dist-packages/urllib3/_collections.py:2: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working\n",
      "  from collections import Mapping, MutableMapping\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "paddle 2.1.0\n"
     ]
    }
   ],
   "source": [
    "import paddle\n",
    "\n",
    "print(\"paddle \" + paddle.__version__)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.8/dist-packages/ipykernel/ipkernel.py:283: DeprecationWarning: `should_run_async` will not call `transform_cell` automatically in the future. Please pass the result to `transformed_cell` argument and any exception that happen during thetransform in `preprocessing_exc_tuple` in IPython 7.17 and above.\n",
      "  and should_run_async(code)\n",
      "/usr/local/lib/python3.8/dist-packages/paddle/tensor/creation.py:125: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. \n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  if data.dtype == np.object:\n"
     ]
    }
   ],
   "source": [
    "x_data = paddle.to_tensor([[1.0], [3.0], [5.0], [9.0], [10.0], [20.0]])\n",
    "y_data = paddle.to_tensor([[12.0], [16.0], [20.0], [28.0], [30.0], [50.0]])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w before optimize: -1.2230666875839233\n",
      "b before optimize: 0.0\n"
     ]
    }
   ],
   "source": [
    "linear = paddle.nn.Linear(in_features=1, out_features=1)\n",
    "\n",
    "w_before_opt = linear.weight.numpy().item()\n",
    "b_before_opt = linear.bias.numpy().item()\n",
    "\n",
    "print(\"w before optimize: {}\".format(w_before_opt))\n",
    "print(\"b before optimize: {}\".format(b_before_opt))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "mse_loss = paddle.nn.MSELoss()\n",
    "sgd_optimizer = paddle.optimizer.SGD(\n",
    "    learning_rate=0.001, parameters=linear.parameters()\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 0 loss [1681.871]\n",
      "epoch 1000 loss [7.9048495]\n",
      "epoch 2000 loss [1.7675705]\n",
      "epoch 3000 loss [0.39517456]\n",
      "epoch 4000 loss [0.08840889]\n",
      "finished training， loss [0.01979099]\n"
     ]
    }
   ],
   "source": [
    "total_epoch = 5000\n",
    "for i in range(total_epoch):\n",
    "    y_predict = linear(x_data)\n",
    "    loss = mse_loss(y_predict, y_data)\n",
    "    loss.backward()\n",
    "    sgd_optimizer.step()\n",
    "    sgd_optimizer.clear_grad()\n",
    "\n",
    "    if i % 1000 == 0:\n",
    "        print(\"epoch {} loss {}\".format(i, loss.numpy()))\n",
    "\n",
    "print(\"finished training， loss {}\".format(loss.numpy()))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w after optimize: 2.0185654163360596\n",
      "b after optimize: 9.770989418029785\n"
     ]
    }
   ],
   "source": [
    "w_after_opt = linear.weight.numpy().item()\n",
    "b_after_opt = linear.bias.numpy().item()\n",
    "\n",
    "print(\"w after optimize: {}\".format(w_after_opt))\n",
    "print(\"b after optimize: {}\".format(b_after_opt))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello paddle\n"
     ]
    }
   ],
   "source": [
    "print(\"hello paddle\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
